package com.zhoug.common3.view.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zhoug.common3.R;
import com.zhoug.common3.utils.AppUtils;
import com.zhoug.common3.utils.LogUtils;
import com.zhoug.common3.utils.StringUtils;
import com.zhoug.common3.view.ZLimitSizeLineaLayout;
import com.zhoug.common3.view.ZLimitSizeNestedScrollView;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

/**
 * Dialog的基类
 * 支持圆角,支持宽度充满屏幕,支持标题.支持副标题,支持设置最大高度
 * @Author 35574
 * @Date 2020/7/20
 * @Description
 */
public abstract class ZBaseDialog extends ZDialog {
    private static final String TAG = ">>>ZBaseDialog";
    private static final boolean DEBUG = false;
    private TextView mTvTitle;
    private TextView mTvMessage;
    private View mLine;
    private ZLimitSizeNestedScrollView mMessageContainer;
    private ZLimitSizeLineaLayout mRootContainer;
    /**
     * 标题
     */
    private String title;
    /**
     * 消息提示
     */
    private String message;
    /**
     * 标题颜色
     */
    private int titleColor;
    /**
     * 标题大小
     */
    private float titleSize;
    /**
     * 提示颜色
     */
    private int messageColor;
    /**
     * 提示大小
     */
    private float messageSize;
    /**
     * message文本的最大高度,超出后滑动查看
     * 默认屏幕高度的一半
     */
    private int maxMessageHeight;

    /**
     * 允许的最大高度:<=0不限制
     */
    private int maxHeight;
    /**
     * 最小高度
     */
    private int minHeight;

    /**
     * 是否充满宽度,左右不留空隙
     */
    private boolean fullWidth;

    /**
     * 是否显示圆角,
     * <=0不显示,>0显示
     */
    private int corners;
    /**
     * 是否显示标题的下划线
     */
    private boolean showTitleLine=true;

    public ZBaseDialog(@NonNull Context context) {
        this(context, 0);
    }

    public ZBaseDialog(@NonNull Context context, @StyleRes int themeResId) {
        this(context, themeResId,0,0);
    }

    protected ZBaseDialog(@NonNull Context context, int themeResId, int defStyleAttr, int defStyleRes) {
        super(context, themeResId, defStyleAttr, defStyleRes);
    }

    @Override
    protected void initialize(@NonNull Context context,  int defStyleAttr, int defStyleRes) {
        maxMessageHeight = AppUtils.getScreenSize().y / 2;
        super.initialize(context, defStyleAttr, defStyleRes);
        DialogAttributeParser.parseZBaseDialogAttrs(this,context,defStyleAttr,defStyleRes);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        mRootContainer = (ZLimitSizeLineaLayout) layoutInflater.inflate(R.layout.z_base_dialog, null);
        mTvTitle = mRootContainer.findViewById(R.id.tv_title);
        mTvMessage = mRootContainer.findViewById(R.id.tv_message);
        mLine = mRootContainer.findViewById(R.id.title_line);
        mMessageContainer = mRootContainer.findViewById(R.id.message_container);
        FrameLayout container = mRootContainer.findViewById(R.id.container);
        View ContentView = layoutInflater.inflate(getLayoutResId(), null);
        container.addView(ContentView);
        setContentView(mRootContainer);
    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        init(savedInstanceState);
        if (getMaxHeight() > 0) {
            mRootContainer.setMaxHeight(getMaxHeight());

        }
        if(getMinHeight()>0){
            mRootContainer.setMinimumHeight(getMinHeight());
        }
        //标题
        if (!StringUtils.isNullOrEmpty(title)) {
            mTvTitle.setVisibility(View.VISIBLE);
            mTvTitle.setText(title);
            setTextColor(mTvTitle, titleColor);
            setTextSize(mTvTitle, titleSize);
        } else {
            mTvTitle.setVisibility(View.GONE);
        }
        //消息
        initMessage();
        //是否显示标题分割线
        if (showTitleLine && (!StringUtils.isNullOrEmpty(title) || !StringUtils.isNullOrEmpty(message))) {
            mLine.setVisibility(View.VISIBLE);
        } else {
            mLine.setVisibility(View.GONE);
        }
        setWindowBackground();//背景圆角
    }

    protected abstract void init(Bundle savedInstanceState);


    /**
     * 消息
     */
    private void initMessage() {
        if (!StringUtils.isNullOrEmpty(message)) {
            mTvMessage.setText(message);
            mTvMessage.setVisibility(View.VISIBLE);
            setTextColor(mTvMessage, messageColor);
            setTextSize(mTvMessage, messageSize);
            //长文本,限制最大高度
            mMessageContainer.setMaxHeight(maxMessageHeight);
        } else {
            mTvMessage.setVisibility(View.GONE);
        }
    }

    /**
     * 根据fullWidth和corners设置窗口背景
     */
    protected void setWindowBackground() {
        if (!fullWidth && corners <= 0) {
            if (DEBUG) {
                LogUtils.d(TAG, "setWindowBackground:不设置");
            }
            return;
        }
        Window window = getWindow();
        if (window != null) {
            //部分手机(例:OPPO)不设置padding为0,还是不会充满屏幕
            View decorView = window.getDecorView();
            decorView.setPadding(0,0,0,0);
            GradientDrawable drawable = new GradientDrawable();
            drawable.setColor(Color.WHITE);
            if (corners > 0) {
                //圆角窗口
                drawable.setCornerRadius(corners);
                if (DEBUG) {
                    LogUtils.d(TAG, "setWindowBackground:圆角窗口");
                }
            }
            if (fullWidth) {
                //充满屏幕
                setWidth(WindowManager.LayoutParams.MATCH_PARENT);
                if (DEBUG) {
                    LogUtils.d(TAG, "setWindowBackground:充满屏幕");
                }
            }
            window.setBackgroundDrawable(drawable);
        }
    }

    @Override
    public void show() {
        super.show();
    }

    /**
     * 最大高度
     *
     * @return 最大高度
     */
    public int getMaxHeight() {
        return maxHeight;
    }

    /**
     * 最大高度
     *
     * @param maxHeight 最大高度
     */
    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }

    public int getMinHeight() {
        return minHeight;
    }

    public void setMinHeight(int minHeight) {
        this.minHeight = minHeight;
    }

    /**
     * 宽度充满屏幕
     *
     * @param fullWidth
     */
    public ZBaseDialog setFullWidth(boolean fullWidth) {
        this.fullWidth = fullWidth;
        return this;
    }

    public boolean isFullWidth() {
        return fullWidth;
    }

    /**
     * 窗口角度
     *
     * @param corners
     */
    public ZBaseDialog setCorners(int corners) {
        this.corners = corners;
        return this;
    }

    public int getCorners() {
        return corners;
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public ZBaseDialog setTitle(String title) {
        this.title = title;
        return this;
    }


    /**
     * 设置标题
     *
     * @param title
     * @param showTitleLine 是否显示标题分割线
     */
    public ZBaseDialog setTitle(String title,boolean showTitleLine) {
        this.title = title;
        this.showTitleLine = showTitleLine;
        return this;
    }

    /**
     * 设置消息内容
     *
     * @param message
     */
    public ZBaseDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    /**
     * 标题颜色
     *
     * @param color
     * @return
     */
    public ZBaseDialog setTitleColor(int color) {
        this.titleColor = color;
        return this;
    }

    /**
     * 标题大小
     *
     * @param titleSize
     * @return
     */
    public ZBaseDialog setTitleSize(float titleSize) {
        this.titleSize = titleSize;
        return this;
    }

    /**
     * 提示颜色
     *
     * @param messageColor
     * @return
     */
    public ZBaseDialog setMessageColor(int messageColor) {
        this.messageColor = messageColor;
        return this;
    }

    /**
     * 提示大小
     *
     * @param messageSize
     * @return
     */
    public ZBaseDialog setMessageSize(float messageSize) {
        this.messageSize = messageSize;
        return this;
    }

    /**
     * message的最大高度
     *
     * @param maxHeight
     */
    public void setMaxMessageHeight(int maxHeight) {
        this.maxMessageHeight = maxHeight;
    }

    protected ZLimitSizeLineaLayout getRootContainer() {
        return mRootContainer;
    }

    /**
     * 设置TextView的文字颜色
     * @param view
     * @param color
     */
    protected static void setTextColor(TextView view, int color) {
        if (view != null && color != 0) {
            view.setTextColor(color);
        }
    }
    /**
     *  设置TextView的文字大小
     * @param view
     * @param textSize
     */
    protected static void setTextSize(TextView view, float textSize) {
        if (view != null && textSize > 0) {
            view.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
        }
    }
}
